#include "TitleScreen.h"

// START: SINGLETON SETUP
TitleScreen* TitleScreen::title_screen = NULL;

TitleScreen* TitleScreen::getSingleton()
{
	if(title_screen == NULL)
		title_screen= new TitleScreen();
	return title_screen;
}
// END: SINGLETON SETUP

void TitleScreen::enter()
{
	// Goes into a scene. The scene manager singleton is called to switch to a scene
	// Passing the name of the scene and the player object, although the player object
	// does not exist for this scene.
	 DefaultScene* defaultScene = new DefaultScene();
	 ProtoScene* protoScene = new ProtoScene();

	  ObjectSceneManager::getSingleton()->switchToScene("ProtoScene", NULL);
	  RenderManager::getSingleton()->getRoot()->addFrameListener(this);

	  m_sName = "ProtoScene";

	  pos = Ogre::Vector3::ZERO;
}

void TitleScreen::exit()
{
}

void TitleScreen::pause()
{
}

void TitleScreen::resume()
{
}


void TitleScreen::update()
{
}


bool TitleScreen::updateKey()
{
	pos = Ogre::Vector3::ZERO;
	if(m_pKeyboard->isKeyDown(OIS::KC_A))
		pos.x = - 0.1;

	if(m_pKeyboard->isKeyDown(OIS::KC_D))
		pos.x = 0.1;

	if(m_pKeyboard->isKeyDown(OIS::KC_W))
		pos.z = - 0.1;

	if(m_pKeyboard->isKeyDown(OIS::KC_S))
		pos.z = 0.1;

	if(m_pKeyboard->isKeyDown(OIS::KC_R))
		ObjectSceneManager::getSingleton()->getCamera()->setPolygonMode(Ogre::PM_WIREFRAME);
	if(m_pKeyboard->isKeyDown(OIS::KC_T))
		ObjectSceneManager::getSingleton()->getCamera()->setPolygonMode(Ogre::PM_SOLID);
	if(m_pKeyboard->isKeyDown(OIS::KC_Y))
		ObjectSceneManager::getSingleton()->getCamera()->setPolygonMode(Ogre::PM_POINTS);

	if(m_pKeyboard->isKeyDown(OIS::KC_ESCAPE))
		return false;

	ObjectSceneManager::getSingleton()->getCamera()->moveRelative(pos);

	return true;
}

bool TitleScreen::updateMouse()
{
	const OIS::MouseState &ms = m_pMouse->getMouseState();
	Ogre::Radian rotX, rotY;
	if(ms.buttonDown(OIS::MB_Right))
	{
		pos.x += ms.X.rel * 0.13;
		pos.y -= ms.Y.rel * 0.13;
	}
	else
	{
		rotX = Ogre::Degree(-ms.X.rel * 0.13);
		rotY = Ogre::Degree(-ms.Y.rel * 0.13);
	}
	ObjectSceneManager::getSingleton()->getCamera()->yaw(rotX);
	ObjectSceneManager::getSingleton()->getCamera()->pitch(rotY);
	ObjectSceneManager::getSingleton()->getCamera()->moveRelative(pos);

	return true;
}

bool TitleScreen::updateJoy()
{
	return true;
}


bool TitleScreen::keyPressed(const OIS::KeyEvent &evt)
{
	return true;
}

bool TitleScreen::keyReleased(const OIS::KeyEvent &evt)
{
	if(evt.key == OIS::KC_ESCAPE)
	{
	}
	return true;
}


bool TitleScreen::frameStarted(const Ogre::FrameEvent& evt)
{
	if(RenderManager::getSingleton()->getRenderWindow()->isClosed()) return false;
	capture();
	update();
	if(!updateKey())
		return false;

	if(!updateMouse())
		return false;
	return true;
}
bool TitleScreen::frameEnded(const Ogre::FrameEvent& evt){ return true; }


// Mouse Listener functions
bool TitleScreen::mouseMoved(const OIS::MouseEvent &evt){ return true; }
bool TitleScreen::mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID){ return true; }
bool TitleScreen::mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID){ return true; }


// Joystick listener functions
bool TitleScreen::buttonPressed(const OIS::JoyStickEvent &evt, int index){ return true; }
bool TitleScreen::buttonReleased(const OIS::JoyStickEvent &evt, int index){ return true; }
bool TitleScreen::axisMoved(const OIS::JoyStickEvent &evt, int index){ return true; }
bool TitleScreen::povMoved(const OIS::JoyStickEvent &evt, int index){ return true; }